﻿package mortal.game.manager.window3d
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.display.*;
    import flash.display3D.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.event.*;
    import frEngine.manager.*;
    import frEngine.primitives.object2d.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.entity.*;

    public class Rect3DObject extends Object
    {
        public var maskRect:Rectangle;
        public var id:uint;
        public var rect:Rectangle;
        private var _areaWidth:int = 0;
        private var _areaHeight:int = 0;
        private var startPoint:Point;
        private var _hasDispose:Boolean = true;
        public var layer:int = -1;
        private var modleUrl:Object;
        private var _bg3dList:Vector.<Img2D>;
        private var _obj3dList:Vector.<Obj3dInfo>;
        public var maskShape:Shape;
        public var maskShape2:Shape;
        public var window:IWindow3D;
        public var hasClose:Boolean = true;
        public var isHide:Boolean = true;
        public var useMask:Boolean = true;
        public var allTimeIsTop:Boolean = false;
        public var renderList:RenderList;
        private var _subRectList:Vector.<Rect3DMask>;
        private static var _id:uint = 0;

        public function Rect3DObject(param1:Rectangle, param2:IWindow3D, param3:Boolean)
        {
            this.rect = new Rectangle();
            this.startPoint = new Point();
            this._bg3dList = new Vector.<Img2D>;
            this._obj3dList = new Vector.<Obj3dInfo>;
            this.maskShape = new Shape();
            this.maskShape2 = new Shape();
            this.renderList = new RenderList();
            this._subRectList = new Vector.<Rect3DMask>;
            this.id = _id + 1;
            this.reInit(param1, param2, param3);
            return;
        }// end function

        public function getInRenderTexture(param1:Array, param2:Array) : void
        {
            var _loc_3:Obj3dInfo = null;
            var _loc_4:Img2D = null;
            for each (_loc_3 in this._obj3dList)
            {
                
                if (_loc_3.obj3d)
                {
                    Resource3dManager.renderingTexture(_loc_3.obj3d, param1, param2);
                }
            }
            for each (_loc_4 in this._bg3dList)
            {
                
                Resource3dManager.renderingTexture(_loc_4, param1, param2);
            }
            return;
        }// end function

        public function render(param1:Scene3D) : void
        {
            var _loc_2:Obj3dInfo = null;
            var _loc_3:Vector.<Layer3DSort> = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Layer3DSort = null;
            var _loc_7:Vector.<Mesh3D> = null;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:Mesh3D = null;
            param1.context.clear(0, 0, 0, 1, 1, 0, Context3DClearMask.DEPTH);
            for each (_loc_2 in this._obj3dList)
            {
                
            }
            _loc_3 = this.renderList.layers;
            _loc_4 = _loc_3.length;
            _loc_5 = 0;
            while (_loc_5 < _loc_4)
            {
                
                _loc_6 = _loc_3[_loc_5];
                if (_loc_6.isActive)
                {
                    _loc_6.sort();
                }
                _loc_7 = _loc_6.list;
                _loc_8 = _loc_7.length;
                if (_loc_8 > 0)
                {
                    _loc_9 = 0;
                    while (_loc_9 < _loc_8)
                    {
                        
                        _loc_10 = _loc_7[_loc_9];
                        _loc_10.draw(false);
                        _loc_9++;
                    }
                }
                _loc_5++;
            }
            _loc_4 = this._subRectList.length;
            _loc_5 = 0;
            while (_loc_5 < _loc_4)
            {
                
                this._subRectList[_loc_5].render(param1);
                _loc_5++;
            }
            return;
        }// end function

        public function removeImg(param1:Img2D, param2:Boolean = true) : void
        {
            var _loc_3:* = this._bg3dList.indexOf(param1);
            if (_loc_3 != -1)
            {
                this._bg3dList.splice(_loc_3, 1);
            }
            return;
        }// end function

        public function addImg(param1:Img2D, param2:int = -30, param3:Rect3DMask = null) : void
        {
            if (!param1 || param1.isDisposed)
            {
                return;
            }
            var _loc_4:* = this._bg3dList.indexOf(param1);
            if (_loc_4 == -1)
            {
                this._bg3dList.push(param1);
            }
            param1.renderList = param3 ? (param3.renderList) : (this.renderList);
            param1.setLayer(param2, false);
            if (!param1.scene)
            {
                param1.addedToScene(Global3D.scene);
            }
            this.moveImg(param1);
            return;
        }// end function

        public function disposeObj3d(param1:Pivot3D) : void
        {
            var _loc_3:int = 0;
            if (!param1)
            {
                return;
            }
            FrEventDispatcher.instance.proxyRemoveEventListener(param1, Engine3dEventName.PIVOT3D_DISPOSE, this.disposeHander);
            var _loc_2:* = this.getMesh3dInfo(param1);
            if (_loc_2)
            {
                _loc_3 = this._obj3dList.indexOf(_loc_2);
                this._obj3dList.splice(_loc_3, 1);
                _loc_2.dispose();
            }
            else
            {
                param1.dispose();
                param1.removedFromScene();
            }
            return;
        }// end function

        public function getMesh3dInfo(param1:Pivot3D) : Obj3dInfo
        {
            var _loc_2:Obj3dInfo = null;
            for each (_loc_2 in this._obj3dList)
            {
                
                if (_loc_2.obj3d == param1)
                {
                    return _loc_2;
                }
            }
            return null;
        }// end function

        public function clearRect3dMask(param1:Rect3DMask) : void
        {
            var _loc_2:* = this._subRectList.indexOf(param1);
            if (_loc_2 != -1)
            {
                this._subRectList.splice(_loc_2, 1);
            }
            return;
        }// end function

        public function createRect3dMask(param1:Rectangle) : Rect3DMask
        {
            var _loc_2:Rect3DMask = null;
            if (!param1)
            {
                return null;
            }
            var _loc_3:* = this._subRectList.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_2 = this._subRectList[_loc_4];
                if (_loc_2.clipRect == param1 || _loc_2.clipRect.equals(param1))
                {
                    return _loc_2;
                }
                _loc_4++;
            }
            _loc_2 = new Rect3DMask();
            this._subRectList.push(_loc_2);
            _loc_2.clipRect = param1;
            _loc_2.modlelocalPos.x = param1.x;
            _loc_2.modlelocalPos.y = param1.y;
            _loc_2.moveRect(this.rect, Global3D.scene.viewPort);
            return _loc_2;
        }// end function

        private function disposeHander(event:Event) : void
        {
            var _loc_4:int = 0;
            var _loc_2:* = event.currentTarget as Pivot3D;
            var _loc_3:* = this.getMesh3dInfo(_loc_2);
            if (_loc_3)
            {
                _loc_4 = this._obj3dList.indexOf(_loc_3);
                this._obj3dList.splice(_loc_4, 1);
                _loc_2.removedFromScene();
                if (_loc_2 is Mesh3D)
                {
                    Mesh3D(_loc_2).renderList = null;
                }
            }
            return;
        }// end function

        public function addObject3d(param1:Pivot3D, param2:int, param3:int, param4:Boolean = true, param5:Rect3DMask = null, param6:Boolean = true) : Obj3dInfo
        {
            if (!param1 || param1.isDisposed)
            {
                return null;
            }
            var _loc_7:* = this.getMesh3dInfo(param1);
            if (!_loc_7)
            {
                _loc_7 = new Obj3dInfo(param1, this);
                FrEventDispatcher.instance.proxyAddEventListener(param1, Engine3dEventName.PIVOT3D_DISPOSE, this.disposeHander);
                this._obj3dList.push(_loc_7);
            }
            _loc_7.autoDispose = param6;
            _loc_7.modlelocalPos.x = param2;
            _loc_7.modlelocalPos.y = param3;
            var _loc_8:* = param5 ? (param5.renderList) : (this.renderList);
            if (param1 is Mesh3D)
            {
                Mesh3D(param1).renderList = _loc_8;
                var _loc_9:* = TimeControler.defaultTimeControler;
                param1.timerContorler = TimeControler.defaultTimeControler;
            }
            else if (param1 is UserModelPlayer)
            {
                UserModelPlayer(param1).setRenderList(_loc_8);
                var _loc_9:* = TimeControler.defaultTimeControler;
                param1.timerContorler = TimeControler.defaultTimeControler;
            }
            if (!param1.scene)
            {
                param1.addedToScene(Global3D.scene);
            }
            this.moveObj3d(_loc_7, Global3D.scene.viewPort);
            return _loc_7;
        }// end function

        public function reInit(param1:Rectangle, param2:IWindow3D, param3:Boolean) : void
        {
            this._hasDispose = false;
            this.useMask = param3;
            this.window = param2;
            this._areaWidth = int(param1.width / 2) * 2 - 2;
            this._areaHeight = int(param1.height / 2) * 2 - 2;
            this.startPoint.x = int(param1.x) + 1;
            this.startPoint.y = int(param1.y) + 1;
            return;
        }// end function

        public function calculateRect() : void
        {
            var _loc_1:* = this.window.localToGlobal(this.startPoint);
            this.rect.x = int(_loc_1.x);
            this.rect.y = int(_loc_1.y);
            this.rect.width = this._areaWidth;
            this.rect.height = this._areaHeight;
            return;
        }// end function

        private function moveImg(param1:Img2D) : void
        {
            var _loc_2:* = Global3D.scene.viewPort;
            param1.setOffsetXY((this.rect.x - 1) - _loc_2.x, (this.rect.y - 1) - _loc_2.y);
            return;
        }// end function

        public function moveObj3d(param1:Obj3dInfo, param2:Rectangle) : void
        {
            var _loc_3:* = param1.obj3d;
            _loc_3.x = param1.modlelocalPos.x + param1.offsetX + this.rect.x - param2.width / 2 - param2.x;
            _loc_3.y = param2.height / 2 - (param1.modlelocalPos.y + param1.offsetY + this.rect.y) + param2.y + Scene3DUtil.UICamera.y;
            return;
        }// end function

        public function moveAll3d() : void
        {
            var _loc_2:Img2D = null;
            var _loc_3:Obj3dInfo = null;
            var _loc_4:Rect3DMask = null;
            var _loc_1:* = Global3D.scene.viewPort;
            for each (_loc_2 in this._bg3dList)
            {
                
                _loc_2.setOffsetXY((this.rect.x - 1) - _loc_1.x, (this.rect.y - 1) - _loc_1.y);
            }
            for each (_loc_3 in this._obj3dList)
            {
                
                this.moveObj3d(_loc_3, _loc_1);
            }
            for each (_loc_4 in this._subRectList)
            {
                
                _loc_4.moveRect(this.rect, _loc_1);
            }
            return;
        }// end function

        private function disposeAllBgImg() : void
        {
            var _loc_1:Img2D = null;
            for each (_loc_1 in this._bg3dList)
            {
                
                if (_loc_1.autoDispose)
                {
                    _loc_1.dispose();
                }
            }
            this._bg3dList.length = 0;
            return;
        }// end function

        private function disposeAllModle3d() : void
        {
            var _loc_1:Obj3dInfo = null;
            for each (_loc_1 in this._obj3dList)
            {
                
                if (_loc_1.autoDispose)
                {
                    _loc_1.dispose();
                }
                FrEventDispatcher.instance.proxyRemoveEventListener(_loc_1.obj3d, Engine3dEventName.PIVOT3D_DISPOSE, this.disposeHander);
            }
            this._obj3dList.length = 0;
            return;
        }// end function

        private function disposeAllSubRect() : void
        {
            this._subRectList.length = 0;
            return;
        }// end function

        public function clear() : void
        {
            this.disposeAllBgImg();
            this.disposeAllModle3d();
            this.disposeAllSubRect();
            this.isHide = true;
            return;
        }// end function

        public function disposeImp() : void
        {
            this.layer = -1;
            this.clear();
            this.renderList.clear();
            this.maskRect = null;
            this._hasDispose = true;
            this.window = null;
            return;
        }// end function

        public function get obj3dList() : Vector.<Obj3dInfo>
        {
            return this._obj3dList;
        }// end function

    }
}
